package edu.indiana.d2i.job;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import edu.indiana.d2i.http.HTRCHttpServer;
import edu.indiana.d2i.job.HTRCJobConfig.JOBTYPE;
import edu.indiana.d2i.vm.VMInfo;
import edu.indiana.d2i.vm.VMPoolManager;
import edu.indiana.d2i.vm.VMStatus;

public class HTRCJobTracker {
	private VMPoolManager   vmpool = null;
	private JobQueueManager queueMgr = null;
	private Thread queueThread;
	private HTRCHttpServer infoServer = null;
	
	private String host = "";
	private int port = 0;
	
	public static final Log LOG = LogFactory.getLog(HTRCJobTracker.class);
	
	private VMInfo[] bootstrapVMPool() {
		// hardcode
		VMInfo[] vmentirelist = new VMInfo[6];
		vmentirelist[0] = new VMInfo("149.165.146.201", "10.0.1.34");
		vmentirelist[1] = new VMInfo("149.165.146.202", "10.0.1.36");
		vmentirelist[2] = new VMInfo("149.165.146.204", "10.0.1.39");
		vmentirelist[3] = new VMInfo("149.165.146.210", "10.0.1.38");
		vmentirelist[4] = new VMInfo("149.165.146.216", "10.0.1.28");
		vmentirelist[5] = new VMInfo("149.165.146.223", "10.0.1.40");		
		return vmentirelist;		
	}
	
	private void loadProperties() throws IOException {
		// 
		InputStream inStream = getClass().getClassLoader().getResourceAsStream("vmm.properties");
		Properties props = new Properties();
		props.load(inStream);
		host = props.getProperty("host");
		port = Integer.valueOf(props.getProperty("port"));
	}
	
	public HTRCJobTracker() throws Exception {		
		loadProperties();
		
		// set up VM pool
		VMInfo[] vmentirelist = bootstrapVMPool();
		vmpool = new VMPoolManager(vmentirelist);
		queueMgr = new JobQueueManager(vmpool, new JobFIFOSchedule());
		
		// set up jetty server
		infoServer = new HTRCHttpServer("job", host, port);
		infoServer.setAttribute("htrcjobtracker", this);
		
		// set up job queue manager
		queueThread = new Thread(queueMgr);
		
		// should move to offerservice()
		infoServer.start();
		queueThread.start();
	}
	
	public String submitJob(HTRCJobConfig jobConf) {
		jobConf.jobID = UUID.randomUUID().toString();
		queueMgr.submitJob(new HTRCJobConfig(jobConf));
		return jobConf.jobID;
	}
	
	public HTRCJobStatus getJobStatus(String jobID) {
		return queueMgr.getJobStatus(jobID);
	}
	
	public HTRCJobInProgress getJobFullInfo(String jobID) {
		return queueMgr.getJobFullInfo(jobID);
	}
	
	public List<VMStatus> getVMPoolStatus() {
		return vmpool.getVMStatus();
	} 
	
	public List<HTRCJobInProgress> getWaitingJobs() {
		return queueMgr.getWaitingJobs();
	}
	
	public List<HTRCJobInProgress> getRunningJobs() {
		return queueMgr.getRunningJobs();
	}
	
	public List<HTRCJobInProgress> getCompletedJobs() {
		return queueMgr.getCompletedJobs();
	}
	
	public void offerService() throws Exception {
//		infoServer.start();
//		queueThread.start();
		
		
		infoServer.join();
		queueThread.join();
	}
	
	public static void main(String[] args) {
		// test logic
		try {
			String userid = "12345";
			String pathname = "./htrcutil.jar";
			String propname = "./nongooglesamples.txt";
//			String propname = "./nongoogle500.txt";
			HTRCJobConfig jobConf = new HTRCJobConfig(userid, pathname, propname, JOBTYPE.MAPREDUCE);
			String[] myargs = {"nongooglesample"};
			jobConf.setArgs(myargs);
			jobConf.setNumVm(3);
			
			HTRCJobTracker tracker = new HTRCJobTracker();
			
			while (true) {
				if (tracker.getWaitingJobs().size() > 1) {
					Thread.sleep(5000);
				} else {
					String jobID1 = tracker.submitJob(jobConf);
					System.out.println("jobID: " + jobID1);
				}
			}
//			tracker.offerService();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
